package com.xnyzc.lhy.order.service.impl.sms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.constant.CommonConstant;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.ConvertUtil;
import com.xnyzc.lhy.common.util.DateUtil;
import com.xnyzc.lhy.order.entity.sms.OaSmsErrorCode;
import com.xnyzc.lhy.order.entity.system.OaSysGlobalConfig;
import com.xnyzc.lhy.order.mapper.sms.OaSmsErrorCodeMapper;
import com.xnyzc.lhy.order.service.sms.IOaSmsErrorCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 验证码错误次数统计 服务实现类
 * </p>
 *
 * @author liuju
 * @since 2019-07-22
 */
@Service
public class OaSmsErrorCodeServiceImpl extends ServiceImpl<OaSmsErrorCodeMapper, OaSmsErrorCode> implements IOaSmsErrorCodeService {

    @Autowired
    OaSmsErrorCodeMapper oaSmsErrorCodeMapper;

    /**
     * 获取用户错误次数
     *
     * @param oaSysGlobalConfig
     * @param phone
     * @param userType
     * @param tag
     * @return
     */
    @Override
    public Boolean getCheckErrorCount(OaSysGlobalConfig oaSysGlobalConfig, String phone, Integer userType, String tag) {

        // 查询当日的错误次数
        QueryWrapper<OaSmsErrorCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaSmsErrorCode.TAG, tag);
        queryWrapper.eq(OaSmsErrorCode.PHONE, phone);
        queryWrapper.eq(OaSmsErrorCode.USER_TYPE, userType);
        queryWrapper.eq(OaSmsErrorCode.ERROR_TIME, DateUtil.getDateDay());
        Integer count = oaSmsErrorCodeMapper.selectCount(queryWrapper);

        if (CheckUtil.objIsEmpty(count)) {
            return true;
        }

        Integer errorNum = CommonConstant.REDIS_SMS_CODE_ERROR_NUM;
        if (!CheckUtil.objIsEmpty(oaSysGlobalConfig.getCodeErrorCount())) {
            errorNum = oaSysGlobalConfig.getCodeErrorCount();
        }
        // 是否超过错误限制
        if (count > errorNum) {
            return false;
        }
        return true;
    }
}
